//
//  BottomBar.m
//  CarTown
//
//  Created by xionghao on 15/8/26.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "BottomBar.h"
#import "SelectedBtn.h"
#define DefalutanmiTime 0.3

@interface BottomBar ()
@property (strong,nonatomic) UIView *otherbgview;
@property (strong,nonatomic) UIButton *postbtn;
@property (nonatomic,assign) CGFloat dy;
@property (nonatomic,assign) BOOL isshowall;
@property (nonatomic,assign) NSInteger counts;
@end

@implementation BottomBar
-(instancetype)initWithBtnTitleArray:(NSArray *)titles WithDelegate:(id<BottomBarBtnClickDelegate>)delegate :(BOOL)isshowall{
    if (self=[super init]) {
       self.frame= CGRectMake(0, XHScreenheight, XHScreenwidth, 54);
        self.backgroundColor=RGBACOLOR(249, 249, 249, 0.95);
        self.layer.borderWidth=0.5;
        self.layer.borderColor=[UIColorWithRGB(202, 208, 207) CGColor];
        UIView *toolbarotherview=[[UIView alloc]initWithFrame:CGRectMake(0, 0 , XHScreenwidth, 54)];
        toolbarotherview.backgroundColor=[UIColor clearColor];
        self.delegate=delegate;
        [self addSubview:toolbarotherview];
        _otherbgview=toolbarotherview;
        _otherbgview.hidden=YES;
        //添加发车源按钮
        UIButton *postcarbtn=[[UIButton alloc]initWithFrame:CGRectMake(16, 8, XHScreenwidth-32, 36)];
        [postcarbtn setBackgroundImage:[UIImage imageWithStretchableName:@"bo_lbian"] forState:UIControlStateNormal];
        [postcarbtn addTarget:self action:@selector(postcar) forControlEvents:UIControlEventTouchUpInside];
        [postcarbtn setTitle:@"发车源" forState:UIControlStateNormal];
        [postcarbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addSubview:postcarbtn];
        _postbtn=postcarbtn;
        self.titlearray=titles;
        _isshowall=isshowall;
        [self setupotherView];
        
    }
    return self;
}


-(void)setupotherView{
    int count=self.titlearray.count;
    CGFloat width;
    if (_isshowall) {
        width=(XHScreenwidth-32-8*count)/(count+1);
    }else{
        width=(XHScreenwidth-32-8*(count-1))/count;
    }
    
    CGFloat x;
    CGFloat y=8;
    CGFloat height=36;
    for (int i=0; i<count; i++) {
        UIButton *btn=[[UIButton alloc]init];
        x=16+(width+8)*i;
        btn.frame=CGRectMake(x, y, width, height);
        [btn setBackgroundImage:[UIImage imageWithStretchableName:@"cancel"] forState:UIControlStateDisabled];
        [btn setBackgroundImage:[UIImage imageWithStretchableName:@"okbg"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitle:self.titlearray[i] forState:UIControlStateNormal];
        btn.tag=i;
        btn.enabled=NO;
        [btn addTarget:self action:@selector(toolbarmenuclick:) forControlEvents:UIControlEventTouchUpInside];
        [self.otherbgview addSubview:btn];
    }
    //初始化全选按钮
    
    SelectedBtn *btn=[[SelectedBtn alloc]initWithFrame:CGRectMake(XHScreenwidth-width-16, 8, width, 36)];
    [btn setImage:[UIImage imageNamed:@"icon_noselect"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"icon_selected"] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(allbtnclick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"全选" forState:UIControlStateNormal];
    [btn setTitleColor:tintblue forState:UIControlStateNormal];
    btn.tag=self.titlearray.count;
    if (self.isshowall) {
        [self.otherbgview addSubview:btn];
    }
}

-(void)setBtnenable:(BOOL)btnenable{
    _btnenable=btnenable;
    for (UIButton *btn in self.otherbgview.subviews) {
        if (btn.tag<(self.isshowall?self.otherbgview.subviews.count-1:self.otherbgview.subviews.count)) {
            btn.enabled=_btnenable;
        }
    }
}
-(void)addInView:(UIView *)view{
    [view addSubview:self];
    self.y=view.height;
    self.dy=view.height;
}

-(void)setPosttile:(NSString *)posttile{
    _posttile=posttile;
    [self.postbtn setTitle:_posttile forState:UIControlStateNormal];
}

-(void)setIshwopost:(BOOL)ishwopost{
    _ishwopost=ishwopost;
    self.postbtn.hidden=!_ishwopost;
    self.otherbgview.hidden=_ishwopost;
}
-(void)setAllselected:(BOOL)allselected{
    _allselected=allselected;
   
        for (UIButton *btn in self.otherbgview.subviews) {
            if (btn.tag==self.otherbgview.subviews.count-1) {
                btn.selected=_allselected;
                self.btnenable=_allselected;
            }
        }
}
-(void)postcar{
    if ([_delegate respondsToSelector:@selector(BottomBarPostClick:)]) {
        [_delegate BottomBarPostClick:self];
    }
}
-(void)toolbarmenuclick:(UIButton *)btn{
    if ([_delegate respondsToSelector:@selector(BottomBar:otherbtnClick:)]) {
        [_delegate BottomBar:self otherbtnClick:btn.tag];
    }
}

-(void)reloadbtn{
    
     [UIView animateWithDuration:DefalutanmiTime animations:^{
          self.y=XHScreenheight;
     } completion:^(BOOL finished) {
         [self.otherbgview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
         [self setupotherView];
         [self show];
     }];
    
}



-(void)allbtnclick:(SelectedBtn *)btn{
    
    if ([_datasource respondsToSelector:@selector(numberofBottobarcount:)]) {
      self.counts= [_datasource numberofBottobarcount:self];
    }
    
    if (self.counts>0) {
        btn.selected=!btn.selected;
        _allselected=btn.selected;
        if (btn.selected) {
            self.btnenable=YES;
        }else{
            self.btnenable=NO;
        }
        if ([_delegate respondsToSelector:@selector(BottomBarAllclick:)]) {
            [_delegate BottomBarAllclick:self];
        }

    }
}
-(void)switchView{
    [UIView animateWithDuration:DefalutanmiTime animations:^{
         self.y=self.dy;
    } completion:^(BOOL finished) {
        self.otherbgview.hidden=!self.otherbgview.hidden;
        self.postbtn.hidden=!self.postbtn.hidden;
        [UIView animateWithDuration:DefalutanmiTime animations:^{
             self.y-=self.height;
        }];
    }];
    
}

-(void)showInView:(UIView *)view{
    [view addSubview:self];
    self.dy=view.height;
    [UIView animateWithDuration:0.3 animations:^{
        self.y=self.dy-self.height;
    }];
    _ishide=NO;
}
-(void)hide{
    [UIView animateWithDuration:0.3 animations:^{
        self.y=self.dy;
    }];
    _ishide=YES;
}
-(void)show{
   
    [UIView animateWithDuration:0.3 animations:^{
        self.y=self.dy-self.height;
    }];
    _ishide=NO;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
